package com.uls.web.controller.found;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.uls.common.core.domain.entity.SysUser;
import com.uls.found.domain.EduRecordRecruitment;
import com.uls.found.service.IEduRecordRecruitmentService;
import com.uls.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.found.domain.EduClaimRecord;
import com.uls.found.service.IEduClaimRecordService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 认领记录Controller
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/found/record")
public class EduClaimRecordController extends BaseController
{
    @Autowired
    private IEduClaimRecordService eduClaimRecordService;

    @Autowired
    private IEduRecordRecruitmentService iEduRecordRecruitmentService;

    @Autowired
    private ISysUserService iSysUserService;


    /**
     * 查询认领记录列表
     */
    @PreAuthorize("@ss.hasPermi('found:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduClaimRecord eduClaimRecord)
    {
        startPage();
        List<EduClaimRecord> list = eduClaimRecordService.selectEduClaimRecordList(eduClaimRecord);

        for (EduClaimRecord eduClaimRecord1:list){

            EduRecordRecruitment eduRecordRecruitment1 =iEduRecordRecruitmentService.selectEduRecordRecruitmentByFoundId(Long.valueOf(eduClaimRecord1.getFoundId()));

            eduClaimRecord1.setFoundname(eduRecordRecruitment1.getFoundName());
        }

        for (EduClaimRecord eduClaimRecord2:list){

            SysUser sysUser=iSysUserService.selectUserById(eduClaimRecord2.getStudentId());

            eduClaimRecord2.setUserName(sysUser.getUserName());
        }


        return getDataTable(list);
    }

    /**
     * 导出认领记录列表
     */
    @PreAuthorize("@ss.hasPermi('found:record:export')")
    @Log(title = "认领记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduClaimRecord eduClaimRecord)
    {
        List<EduClaimRecord> list = eduClaimRecordService.selectEduClaimRecordList(eduClaimRecord);
        ExcelUtil<EduClaimRecord> util = new ExcelUtil<EduClaimRecord>(EduClaimRecord.class);
        util.exportExcel(response, list, "认领记录数据");
    }

    /**
     * 获取认领记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('found:record:query')")
    @GetMapping(value = "/{claimId}")
    public AjaxResult getInfo(@PathVariable("claimId") Integer claimId)
    {
        return success(eduClaimRecordService.selectEduClaimRecordByClaimId(claimId));
    }

    /**
     * 新增认领记录
     */
    @PreAuthorize("@ss.hasPermi('found:record:add')")
    @Log(title = "认领记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduClaimRecord eduClaimRecord)
    {
        eduClaimRecord.setCreateBy(getUsername().toString());
        eduClaimRecord.setCreateTime(new Date());

        eduClaimRecord.setStudentId(getUserId());
        return toAjax(eduClaimRecordService.insertEduClaimRecord(eduClaimRecord));
    }

    /**
     * 修改认领记录
     */
    @PreAuthorize("@ss.hasPermi('found:record:edit')")
    @Log(title = "认领记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduClaimRecord eduClaimRecord)
    {
        eduClaimRecord.setUpdateBy(getUsername().toString());
        eduClaimRecord.setUpdateTime(new Date());

        eduClaimRecord.setStudentId(getUserId());
        return toAjax(eduClaimRecordService.updateEduClaimRecord(eduClaimRecord));
    }

    /**
     * 删除认领记录
     */
    @PreAuthorize("@ss.hasPermi('found:record:remove')")
    @Log(title = "认领记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{claimIds}")
    public AjaxResult remove(@PathVariable Integer[] claimIds) {

        int total=0;

        for (Integer claimId : claimIds) {

            EduClaimRecord eduClaimRecord = eduClaimRecordService.selectEduClaimRecordByClaimId(claimId);

            if (eduClaimRecord != null) {

                eduClaimRecord.setDeleteBy(getUsername().toString());

                eduClaimRecord.setDeleteTime(new Date());

                eduClaimRecord.setDelFlag(1);

                int a= eduClaimRecordService.updateEduClaimRecord(eduClaimRecord);

                total= total+a;

            }

        }
        return toAjax(total);
    }


    /**
     * 获取招领的全部名称
     */
    @PreAuthorize("@ss.hasPermi('found:record:query')")
    @GetMapping(value = "/getAllEduRecordRecruitments")
    public AjaxResult getInfo()
    {
        AjaxResult ajaxResult=AjaxResult.success();

       List<EduRecordRecruitment> eduRecordRecruitments=  iEduRecordRecruitmentService.selectEduRecordRecruitmentList(null);

       ajaxResult.put("eduRecordRecruitments",eduRecordRecruitments);

       return ajaxResult;
    }



    //下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EduClaimRecord> util = new ExcelUtil<EduClaimRecord>(EduClaimRecord.class);
        util.importTemplateExcel(response, "用户数据");
    }


    @Log(title = "认领管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('found:record:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EduClaimRecord> util = new ExcelUtil<EduClaimRecord>(EduClaimRecord.class);
        List<EduClaimRecord> eduClaimRecordList = util.importExcel(file.getInputStream());
        String message = eduClaimRecordService.importUser(eduClaimRecordList, updateSupport);
        return success(message);
    }

    /**
     * 获取提交详细信息
     */
    @PreAuthorize("@ss.hasPermi('found:record:submit')")
    @GetMapping(value = "/submitClaim/{claimId}")
    public AjaxResult submitEduClaimRecord(@PathVariable("claimId") Integer claimId)
    {
        return toAjax(eduClaimRecordService.submit(claimId,getUserId()));
    }


    /**
     * 查询未完成的失物招领列表
     */
    @PreAuthorize("@ss.hasPermi('found:record:listNo')")
    @GetMapping("/listNo")
    public TableDataInfo listNo(EduClaimRecord eduClaimRecord)
    {
        startPage();
        List<EduClaimRecord> list = eduClaimRecordService.noFinishTask(getUserId(),"myProcess");

      //  List<EduClaimRecord> list = eduClaimRecordService.noFinishTask(getUserId(),"myWorkProcess");
      //  List<EduClaimRecord> list = eduClaimRecordService.noFinishTask(getUserId(),"lossProcess",eduClaimRecord);

        for (EduClaimRecord eduClaimRecord3:list){

            EduRecordRecruitment eduRecordRecruitment1 =iEduRecordRecruitmentService.selectEduRecordRecruitmentByFoundId(Long.valueOf(eduClaimRecord3.getFoundId()));

            eduClaimRecord3.setFoundname(eduRecordRecruitment1.getFoundName());
        }

        for (EduClaimRecord eduClaimRecord4:list){

            SysUser sysUser=iSysUserService.selectUserById(eduClaimRecord4.getStudentId());

            eduClaimRecord4.setUserName(sysUser.getUserName());
        }

        return getDataTable(list);
    }


    /**
     * 获取未完成审批的详细信息
     */
    @GetMapping(value = "/getNoRecordOther/{claimId}")
    public AjaxResult getRecordOther(@PathVariable("claimId") Integer claimId)
    {
        return success(eduClaimRecordService.selectEduClaimRecordByClaimId(claimId));
    }



    /**
     * 审批学生
     */
    @PreAuthorize("@ss.hasPermi('found:record:editnoapprove')")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/approveNoRecord")
    public AjaxResult approveRecordData(@RequestBody EduClaimRecord eduClaimRecord)
    {
        eduClaimRecordService.competeTast(eduClaimRecord.getFlowid(),eduClaimRecord.getAgree(),getUserId(),eduClaimRecord.getComment(),eduClaimRecord.getClaimId());
        return toAjax(200);
    }


    /**
     * 查询已完成的失物招领列表
     */
    @PreAuthorize("@ss.hasPermi('found:record:listFinished')")
    @GetMapping("/listFinished")
    public TableDataInfo listfinishRecord(EduClaimRecord eduClaimRecord)
    {
        startPage();
        List<EduClaimRecord> list = eduClaimRecordService.finishTask(getUserId(),"myProcess");
       // List<EduClaimRecord> list = eduClaimRecordService.finishTask(getUserId(),"myWorkProcess");

        for (EduClaimRecord eduClaimRecord5:list){

            EduRecordRecruitment eduRecordRecruitment1 =iEduRecordRecruitmentService.selectEduRecordRecruitmentByFoundId(Long.valueOf(eduClaimRecord5.getFoundId()));

            eduClaimRecord5.setFoundname(eduRecordRecruitment1.getFoundName());
        }

        for (EduClaimRecord eduClaimRecord6:list){

            SysUser sysUser=iSysUserService.selectUserById(eduClaimRecord6.getStudentId());

            eduClaimRecord6.setUserName(sysUser.getUserName());
        }

        return getDataTable(list);
    }

    /**
     * 获取完成学生的详细信息批注
     */
    @GetMapping(value = "/getEduClaimRecordComment/{claimId}")
    public AjaxResult getCommerntEduClaimRecord(@PathVariable("claimId") Integer claimId)
    {
        EduClaimRecord eduClaimRecord= eduClaimRecordService.selectEduClaimRecordByClaimId(claimId);
        eduClaimRecord.setTaskCommentVoList(eduClaimRecordService.getTaskCommentList(eduClaimRecord.getFlowid()));
        return success(eduClaimRecord);
    }






}
